﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
using System.IO;

public partial class admin_Dangnhap : System.Web.UI.Page
{
    #region "Member variables"
    int intLanguageID;
    DANguoidung objNguoidung;
    DALog objLog;
    DANguoiDungStation objNguoiDungStation;
    SqlConnection sqlConnec = ConnectData.getConnection();
    #endregion
    #region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion     
    #region "Private methods"
    // Dat lai ten, mat khau
    private void ResetForm()
    {
        this.txtUserName.Text = "";
        this.txtPassWord.Text = "";
        this.ctvLogin.IsValid = true;
    }
    // Dang nhap he thong
    private void LoginSystem()
    {
        if (Page.IsValid)
        {
            string strUserName = this.txtUserName.Text.Trim();
            string strPassWord = this.txtPassWord.Text.Trim();
            strPassWord = FormsAuthentication.HashPasswordForStoringInConfigFile(strPassWord, "SHA1");
            CheckAccount(strUserName, strPassWord);
        }
    }
    // Kiem tra cap ten/mat khau
    public void CheckAccount(string strUserName, string strPassWord)
    {
        int intUserID = objNguoidung.KiemTraMatKhau(strUserName, strPassWord);
        if (intUserID > 0)
        {
            if (this.cbxRememberPassword.Checked)
            {
                Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(1);
                Response.Cookies["PassWord"].Expires = DateTime.Now.AddMonths(1);
                Response.Cookies["UserName"].Value = strUserName;
                Response.Cookies["PassWord"].Value = strPassWord;
            }
            Session["AccountAdmin"] = "True";
            Session["Username"] = strUserName;
            Session["UserID"] = intUserID;
            Session["QuyenUser"] = checkQuyenUser(intUserID, strUserName);
            Session["StationCode"] = load_station_code_by_user();
            objLog.insertLog(0, 0, 1, intUserID);
            DataSet dsNguoiDung = objNguoidung.loadNguoiDungChucNang(intUserID);
            foreach (DataRow childRow in dsNguoiDung.Tables[0].Rows)
            {
                Response.Redirect("Default.aspx?ChucNang=" + childRow["ID"].ToString() + "&UserList=" + intUserID.ToString());
            }
        }
        else
        {
            this.ctvLogin.IsValid = false;
        }
    }
    private string load_station_code_by_user()
    {
        string strStationCode = "";
        if (Session["UserID"] != null)
        {
            DataSet v_ds = new DataSet();
            objNguoiDungStation = new DANguoiDungStation(sqlConnec);
            v_ds = objNguoiDungStation.load_station_by_nguoidung(Convert.ToInt32(Session["UserID"]));
            if (v_ds.Tables[0].Rows.Count > 0)
                strStationCode = v_ds.Tables[0].Rows[0]["StationCode"].ToString();
        }
        return strStationCode;
    }
    private bool check_quyen_xoa_umg_vien(string strUserName)
    {
        string v_str_duong_dan_file;
        int v_i_flag = 0;
        v_str_duong_dan_file = Server.MapPath("~/UserControl/HocVien/DS_tai_khoan_duoc_xoa_cts_quan_ly_ko_xuat_data.txt");
        string[] v_str_danh_sach_nguoi_dung_duoc_xoa = File.ReadAllLines(v_str_duong_dan_file);
        // Account hiện đang đăng nhập
        string v_str_account = strUserName;
        for (int v_i = 0; v_i < v_str_danh_sach_nguoi_dung_duoc_xoa.Length; v_i++)
        {
            if (v_str_danh_sach_nguoi_dung_duoc_xoa[v_i].Equals(v_str_account))
            {
                v_i_flag = 1;  // nghĩa là được xóa
                break;
            }
        }
        if (v_i_flag > 0) return true;  // Account được xóa
        return false; // User ko được xóa
    }
    private bool checkQuyenUser(int UserID, string strUserName)
    {
        if (UserID == 1 || check_quyen_xoa_umg_vien(strUserName))
        {
            return true;
        }
        else
        {
            DataSet dsNguoidung = objNguoidung.loadQuyenNguoidungbyNguoiDung(UserID);
            if (dsNguoidung != null && dsNguoidung.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dsNguoidung.Tables[0].Rows)
                {
                    Session["QuyenID"] = dr["QuyenID"];
                    if (dr["QuyenID"].ToString().Equals("1") || dr["QuyenID"].ToString().Equals("2"))
                    {
                        return true;

                    }
                }
            }
        }
        return false;
    }
    #endregion
    #region "Events handle"
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objNguoidung = new DANguoidung(sqlConnec);
        objLog = new DALog(sqlConnec);
    }
    // Main void
    private void Page_Load(object sender, EventArgs e)
    {
        // if (ConnectionView != null)
        this.lblPageTitle.Text = ConfigurationSettings.AppSettings["Title"] != null ? ConfigurationSettings.AppSettings["Title"].ToString() : "";
        {
            // Put user code to initialize the page here
            intLanguageID = Convert.ToInt32(Session["LanguageCode"]);
            loadDataAccess();
            if (Request.Cookies["UserName"] != null && Request.Cookies["PassWord"] != null)
            {
                Response.Cookies.Set(Request.Cookies["UserName"]);
                Response.Cookies.Set(Request.Cookies["PassWord"]);
                if (!Response.Cookies["UserName"].Value.Equals("") && !Response.Cookies["PassWord"].Value.Equals(""))
                {
                    this.txtUserName.Text = Response.Cookies["UserName"].Value.ToString().Trim();
                    this.txtPassWord.Text = Response.Cookies["PassWord"].Value.ToString().Trim();
                    CheckAccount(Request.Cookies["UserName"].Value, Request.Cookies["PassWord"].Value);
                }
            }
        }
    }
    // Dat lai ten, mat khau
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        this.ctvLogin.IsValid = true;
        LoginSystem();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        ResetForm();
    }
    #endregion
}
